package org.nepu.scr.service;

import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import static org.nepu.scr.constant.SikeConstant.*;
@Slf4j
@Service
@AllArgsConstructor
public class AnalyseService {

    //分析算法
    @SneakyThrows
    public String analyse(Double[][] matrix,Double[][] score){

        // 对角线上的数字1
        for(int i=0;i<matrix.length;i++){
            matrix[i][i]=1.0;
        }

        //根据输入值自动填写矩阵剩余项
        for(int i= matrix.length-1;i>=0;i--){
            for(int j=matrix.length-1;j>=0;j--){
                matrix[i][j]=1/matrix[j][i];
            }
        }



//        printMatrix(matrix);
        Double[][] matrixColumn;

        matrixColumn=normalCol(matrix);// 矩阵的列归一化
//        printMatrix(matrixColumn);

        //特征权重表
        Double[] w;
        w=normalLine(matrixColumn); // 矩阵的行归一化


        Double[] bw;
        bw=mutiMat(matrix,w);  //B*W 矩阵相乘
        Double sumR=0.0;                        //最大特征跟R
        for(int i=0;i<matrix.length;i++){
            sumR=sumR+bw[i]/(matrix.length*w[i]);
        }

//        for (int i = 0;i<w.length;i++){
//            log.info("++++++++++"+w[i]);
//        }

//        Double[][] score = {
//                {0.0,0.0,1.0,0.0,0.0},
//                {0.0,0.0,1.0,0.0,0.0},
//                {0.0,0.0,1.0,0.0,0.0},
//                {0.0,0.0,1.0,0.0,0.0},
//                {0.0,0.0,1.0,0.0,0.0}
//        };

//        printMatrix(score);

        Double[] result = mutiMat(score,w);
//        for (int i = 0; i < result.length; i++) {
//            log.info("result"+result[i]);
//        }
        double max = result[0];
        int maxIndex = 0;

        // 遍历数组，从第二个元素开始
        for (int i = 1; i < result.length; i++) {
            // 如果发现更大的数，则更新最大值和下标
            if (result[i] > max) {
                max = result[i];
                maxIndex = i;
            }
        }

//        log.info("+++++++++"+result[maxIndex]);
//        return result[maxIndex];
        if(maxIndex==4){
            return FIVEG;
        }else if (maxIndex==3){
            return FOREG;
        }else if (maxIndex==2){
            return THREEG;
        }else if (maxIndex==1){
            return TWOG;
        }else{
            return ONEG;
        }

//        Double ci=(sumR-matrix.length)/(matrix.length-1);   //矩阵一致性指标


//        Double cr = ci/grade[matrix.length];                 //随机一致性比率
//        System.out.println("======================================");
//        System.out.println("最大特征值:"+sumR+" , 一致性指标CI:"+ci+" , 一致性比例CR: "+cr);




//        if(cr>=0.1){
//            System.out.println("权重设置不合理");
//        }else{
//            //输出特征向量
//            for(int i=0;i<5;i++){
//                System.out.println("特征"+i+"的权重："+w[i]);
//            }
//        }

    }

    //打印矩阵
    @SneakyThrows
    public static void printMatrix(Double[][] matrix){
        System.out.println("矩阵输出：");
        for (int i = 0; i < matrix.length; i++) {
            System.out.print("[");
            for (int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j]+" ");
            }
            System.out.println("]");
        }
    }

    //对矩阵列归一化（每个数除以每列的和），并返回列归一化后的矩阵
    @SneakyThrows
    public static Double[][] normalCol(Double[][] matrix){
//        System.out.println("列归一化：");
        Double[] column=new Double[matrix.length];
        for(int j=0;j<matrix.length;j++){
            for(int i=0;i<matrix.length;i++){
                if(column[j]!=null){
                    column[j]=column[j]+matrix[i][j];
                }else{
                    column[j]=matrix[i][j];
                }
            }

        }
        //矩阵列归一化  每个数除以每列的和
        Double[][] matrixColumn= new Double[matrix.length][matrix.length];
        for(int j=0;j<matrix.length;j++){
            for(int i=0;i<matrix.length;i++){
                matrixColumn[i][j]=matrix[i][j]/column[j];
            }

        }
        return matrixColumn;
    }

    //矩阵的行归一化（逐行相加求行和sum，行和/sum 归一化得最终特征向量）
    @SneakyThrows
    public static  Double[] normalLine(Double[][] matrixColumn ){
//        System.out.println("行归一化：");
        Double[] line = new Double[matrixColumn.length];//行和
        for(int i=0;i<matrixColumn.length;i++){
            for(int j=0;j<matrixColumn.length;j++){
                if(line[i]!=null){
                    line[i]=line[i]+matrixColumn[i][j];
                }else{
                    line[i]=matrixColumn[i][j];
                }
            }

        }
        Double[] w=new Double[line.length];
        Double sum=0.0;
        for(int i=0;i<line.length;i++){
            sum=sum+line[i];//总的行和
        }
        for(int i=0;i<line.length;i++){
            w[i]=line[i]/sum;                    //特征向量
        }
        return w;
    }

    // @TODO 通用房啊
    //矩阵相乘功能 matrix（多维矩阵）  w （一维矩阵） Double[] bw返回一维结果
    @SneakyThrows
    public static  Double[] mutiMat(Double[][] matrix ,Double[] w){
        Double[] bw = new Double[matrix[0].length];
        for(int i=0;i<matrix[0].length;i++){
            for(int j=0;j<matrix.length;j++){
                if(bw[i]!=null){
                    bw[i]+=matrix[j][i]*w[j];
                }else{
                    bw[i]=matrix[j][i]*w[j];
                }
            }
        }

        return bw;
    }


}
